﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using Util;
using System.Collections;

namespace DAO
{
    /// <summary>
    /// Summary description for Photo
    /// </summary>
    public class Photo
    {
        DBConnection DBConnection;
        public Photo()
        {
            DBConnection = new DBConnection();
            //
            // TODO: Add constructor logic here
            //
        }
        /// <summary>
        /// Get all DTO.Photo instance from DB.
        /// </summary>
        /// <returns>List<> of all DTO.Photo instances.</returns>
        public List<DTO.Photo> GetAllPhotos()
        {
            Log.LogInfo("Begin GetAllPhotos()");
            String query = "SELECT * FROM [Photo];";
            SqlDataReader reader = DBConnection.ExecuteReader(query);
            List<DTO.Photo> photoDTOs = new List<DTO.Photo>();
            try
            {
                while (reader.Read())
                {
                    photoDTOs.Add(new DTO.Photo(reader));
                }
            }
            catch (Exception ex)
            {
                Log.LogError("Error GetAllPhotos(): " + ex.Message);
            }
            DBConnection.Close();
            Log.LogInfo("End GetAllPhotos()");
            return photoDTOs;
        }
        /// <summary>
        /// Get DTO.Photo instance from DB.
        /// </summary>
        /// <param name="ID">Photo ID</param>
        /// <returns>DTO.Photo instance which has the specific ID.</returns>
        public DTO.Photo GetPhoto(String ID)
        {
            Log.LogInfo("Begin GetPhoto(String ID)");
            String query = "SELECT * FROM [Photo] WHERE [Photo].[ID]='" + ID + "';";
            SqlDataReader reader = DBConnection.ExecuteReader(query);
            DTO.Photo photoDTO = null;
            try
            {
                if (reader.Read())
                    photoDTO = new DTO.Photo(reader);
            }
            catch (Exception ex)
            {
                Log.LogError("Error GetPhoto(String ID): " + ex.StackTrace);
            }
            DBConnection.Close();
            Log.LogInfo("End GetPhoto(String ID)");
            return photoDTO;
        }

        /// <summary>
        /// Insert Photo into database.
        /// </summary>
        /// <param name="Photo">DTO.Photo instance to insert into database.</param>
        public int InsertPhoto(DTO.Photo Photo)
        {
            Log.LogInfo("Begin InsertPhoto()");
            String query = "";
            query += "INSERT INTO PHOTO (UserID, Album, Name, Description, Link) VALUES ('" + Photo.UserID + "','" + Photo.AlbumID + "','" + Photo.Name + "','" + Photo.Description + "','" + Photo.Link + "')";
            query += "";
            int result = DBConnection.ExecuteNonQuery(query);
            DBConnection.Close();
            Log.LogInfo("Insert " + result.ToString() + "row(s).");
            Log.LogInfo("End InsertPhoto()");
            return result;

        }
        public ArrayList getListPhotoWithAlbum(String userID,int albumID)
        {
            Log.LogInfo("Begin getListPhotoWithAlbum");
            String query = "Select * from Photo where UserID='" + userID + "' and Album='"+albumID.ToString()+"'";
            SqlDataReader reader = DBConnection.ExecuteReader(query);
            DTO.Photo photoDTO = null;
            ArrayList arrayList = new ArrayList();
            try
            {
                while (reader.Read())
                {
                    photoDTO = new DTO.Photo(reader);
                    arrayList.Add(photoDTO);
                }
            }
            catch (Exception ex)
            {
                Log.LogError("Error getListPhotoWithAlbum(String userID,int albumID)");
            }
            Log.LogInfo("End getListPhotoWithAlbum(String userID,int albumID)");
            DBConnection.Close();
            return arrayList;
        }

        /// <summary>
        /// Update Photo info into database.
        /// </summary>
        /// <param name="Photo">Photo instance to be updated.</param>
        public int UpdatePhoto(DTO.Photo Photo)
        {
            Log.LogInfo("Begin UpdatePhoto()");
            String query = "UPDATE PHOTO SET UserID = '" + Photo.UserID + "'," + " Album = '" + Photo.AlbumID + "'Name ='" + Photo.Name + "'Description = '" + Photo.Description + "'Link = '" + Photo.Link + "' WHERE ID = '" + Photo.ID + "'";
            int result = DBConnection.ExecuteNonQuery(query);
            DBConnection.Close();
            Log.LogInfo("Update " + result.ToString() + "row(s).");
            Log.LogInfo("End InsertPhoto()");
            return result;

        }
        /// <summary>
        /// Delete a specific Photo from DB.
        /// </summary>
        /// <param name="Photo">The Photo to be deleted.</param>
        public int DeletePhoto(DTO.Photo Photo)
        {
            Log.LogInfo("Begin DeletePhoto()");
            String query = "DELETE FROM [Photo] WHERE [Photo].[ID]='" + Photo.ID + "';";
            int result = DBConnection.ExecuteNonQuery(query);
            Log.LogInfo("Deleted " + result.ToString() + "row(s).");
            Log.LogInfo("End DeletePhoto()");
            return result;
        }

        /// <summary>
        /// Delete a Photo in database using ID.
        /// </summary>
        /// <param name="ID">Photo ID.</param>
        public int DeletePhoto(String ID)
        {
            Log.LogInfo("Begin DeletePhoto()");
            String query = "DELETE FROM [Photo] WHERE [Photo].[ID]='" + ID + "';";
            int result = DBConnection.ExecuteNonQuery(query);
            Log.LogInfo("Deleted " + result.ToString() + "row(s).");
            Log.LogInfo("End DeletePhoto()");
            return result;
        }

        internal ArrayList getListPhotoWithAlbum(string userID, string albumID)
        {
            throw new NotImplementedException();
        }
    }
}